#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]){
  char filename[] = "aFile"; //即将创建和打开的文件名
  char bufForReadWrite[] = "This is os2Class , I'm dancehole , num202100201047";
  size_t bufSize = strlen(bufForReadWrite);//测量出读写缓冲区大小
  int fd; //用于存放open函数返回的文件描述符
  
  fd = open(filename, O_RDWR | O_CREAT);//创建并打开文件，可读可写，创建权限为644 
  if(fd < 0){
      //创建文件失败时返回
    perror("Unable to create file");
    return -1;
}
ssize_t sizeWrited=write(fd, (void *) bufForReadWrite, bufSize);
printf("Write:%ld bytes to %s\n", sizeWrited, filename);
close(fd); //写入完毕后关闭文件

fd = open(filename, O_RDWR);//打开文件后的权限为可读可写
if(fd < 0){
      //打开文件失败时返回
    perror("Unable to open file");
    return -1;
}
ssize_t sizeReaded=read(fd, (void *) bufForReadWrite, bufSize);
printf("Read:%ld bytes from %s, read content:%s\n", sizeWrited, filename, bufForReadWrite);
close(fd); //读完毕后关闭文件
return 0;
}

